// utils/config-checker.js
// 配置检查工具 - 检查微信小程序配置

class ConfigChecker {
  constructor() {
    this.requiredDomains = [
      'iam.cn-north-4.myhuaweicloud.com',
      'ocr.cn-north-4.myhuaweicloud.com'
    ]
  }

  // 检查项目配置
  checkProjectConfig() {
    console.log('=== 项目配置检查 ===')
    
    try {
      // 检查project.config.json
      const projectConfig = require('../../project.config.json')
      console.log('项目配置:', projectConfig)
      
      return {
        success: true,
        projectConfig: projectConfig,
        libVersion: projectConfig.libVersion,
        setting: projectConfig.setting
      }
    } catch (error) {
      console.log('读取项目配置失败:', error)
      return {
        success: false,
        error: error.message
      }
    }
  }

  // 检查app.json配置
  checkAppConfig() {
    console.log('=== App配置检查 ===')
    
    try {
      const appConfig = require('../../app.json')
      console.log('App配置:', appConfig)
      
      return {
        success: true,
        appConfig: appConfig,
        pages: appConfig.pages,
        permission: appConfig.permission,
        requiredBackgroundModes: appConfig.requiredBackgroundModes
      }
    } catch (error) {
      console.log('读取App配置失败:', error)
      return {
        success: false,
        error: error.message
      }
    }
  }

  // 检查API配置
  checkApiConfig() {
    console.log('=== API配置检查 ===')
    
    try {
      const apiConfig = require('../../config/api.js')
      console.log('API配置:', apiConfig)
      
      return {
        success: true,
        huawei: apiConfig.huawei,
        aliyun: apiConfig.aliyun
      }
    } catch (error) {
      console.log('读取API配置失败:', error)
      return {
        success: false,
        error: error.message
      }
    }
  }

  // 检查系统信息
  checkSystemInfo() {
    console.log('=== 系统信息检查 ===')
    
    try {
      const systemInfo = wx.getSystemInfoSync()
      console.log('系统信息:', systemInfo)
      
      return {
        success: true,
        platform: systemInfo.platform,
        SDKVersion: systemInfo.SDKVersion,
        version: systemInfo.version,
        system: systemInfo.system,
        isDevTools: systemInfo.platform === 'devtools'
      }
    } catch (error) {
      console.log('获取系统信息失败:', error)
      return {
        success: false,
        error: error.message
      }
    }
  }

  // 生成域名配置建议
  generateDomainConfig() {
    console.log('=== 域名配置建议 ===')
    
    const config = {
      requiredDomains: this.requiredDomains,
      requestDomains: this.requiredDomains,
      socketDomains: [],
      uploadDomains: [],
      downloadDomains: []
    }
    
    console.log('建议的域名配置:', config)
    
    return config
  }

  // 运行完整配置检查
  runFullCheck() {
    console.log('=== 完整配置检查开始 ===')
    
    try {
      const projectConfig = this.checkProjectConfig()
      const appConfig = this.checkAppConfig()
      const apiConfig = this.checkApiConfig()
      const systemInfo = this.checkSystemInfo()
      const domainConfig = this.generateDomainConfig()
      
      const checkResult = {
        projectConfig,
        appConfig,
        apiConfig,
        systemInfo,
        domainConfig
      }
      
      console.log('=== 配置检查结果 ===')
      console.log('完整检查结果:', checkResult)
      
      return checkResult
    } catch (error) {
      console.log('配置检查异常:', error)
      throw error
    }
  }

  // 生成配置修复建议
  generateFixSuggestions(checkResult) {
    console.log('=== 生成修复建议 ===')
    
    const suggestions = []
    
    // 检查是否在开发工具中
    if (checkResult.systemInfo.success && checkResult.systemInfo.isDevTools) {
      suggestions.push('✅ 当前在微信开发者工具中，可以忽略域名配置限制')
    } else {
      suggestions.push('⚠️ 需要在微信公众平台配置合法域名')
      suggestions.push('   - 登录微信公众平台')
      suggestions.push('   - 进入"开发" -> "开发管理" -> "开发设置"')
      suggestions.push('   - 在"服务器域名"中添加以下域名:')
      this.requiredDomains.forEach(domain => {
        suggestions.push(`     * ${domain}`)
      })
    }
    
    // 检查基础库版本
    if (checkResult.systemInfo.success) {
      const version = checkResult.systemInfo.SDKVersion
      if (version && version < '2.0.0') {
        suggestions.push('⚠️ 建议升级基础库版本到2.0.0以上')
      }
    }
    
    // 检查项目配置
    if (checkResult.projectConfig.success) {
      const setting = checkResult.projectConfig.setting
      if (setting && !setting.urlCheck) {
        suggestions.push('✅ 已关闭域名校验，开发阶段可以使用')
      } else {
        suggestions.push('⚠️ 建议在开发阶段关闭域名校验')
        suggestions.push('   - 在project.config.json中设置 "urlCheck": false')
      }
    }
    
    console.log('修复建议:', suggestions)
    return suggestions
  }
}

module.exports = new ConfigChecker()
